package org.eliza.base.color


// SetTerminal by given code.
fun SetTerminal(code: String): Error? {
    if (!Enable || !SupportColor()) {
        return null
    }
    System.err.println(code)
    return null
}

// ResetTerminal terminal setting.
fun ResetTerminal(): Error? {
    if (!Enable || !SupportColor()) {
        return null
    }
    System.err.println(ResetSet)
    return null
}

/*************************************************************
 * print methods(will auto parse color tags)
 *************************************************************/

// Print render color tag and print messages

/*************************************************************
 * helper methods
 *************************************************************/


// use Println, will add spaces for each arg
fun formatArgsForPrintln(args: Array<Any>): (String) {
    val ln = args.size
    var message = ""
    message = if (args.isEmpty()) {
        ""
    } else if (ln == 1) {
        args[0].toString()
    } else {
        args.joinToString()
    }
    return message
}


// is on debug mode
// fun isDebugMode() Boolean {
// 	return debugMode == "on"
// }

fun debugf(f: String, vararg v: Any) {
    if (debugMode) {
        println("COLOR_DEBUG: ${f}")
        println(v)
    }
}

// equals: return ok ? val1 : val2
fun isValidUint8(vv: Int): Boolean {
    return vv >= 0 && vv < 256
}

// equals: return ok ? val1 : val2
fun compareVal(ok: Boolean, val1: UInt, val2: UInt): UInt {
    if (ok) {
        return val1
    }
    return val2
}

// equals: return ok ? val1 : val2
fun compareF64Val(ok: Boolean, val1: Float, val2: Float): Float {
    if (ok) {
        return val1
    }
    return val2
}

fun saveInternalError(err: Error?) {
    if (err != null) {
        err.message?.let { debugf("inner error: %s", it) }
    }
}

fun StringToArr(str: String, sep: String): (Array<String>) {
    var str = str.trim()
    var aa = arrayOf<String>()
    if (str == "") {
        return aa
    }

    var ss = str.split(sep)
    ss.forEach { vv ->
        if (vv.length > 0) {
            aa.plus(vv)
        }
    }
    return aa

}
